<?php
namespace Home\Controller;

use Think\Controller;

class MsgController extends CommonController
{
  public function __construct()
  {
    parent::__construct();
    $this->check_login();
  }

  /**
   * [通知商户新订单]
   * @return [type] [description]
   */
  public function new_order()
  {
    $payload = file_get_contents('php://input');
    $data = json_decode($payload, true);

    $order = M('orders')->where("tran_no={$data['tran_no']}")->find();
    $order_child = M('order_children')->where("order_id={$order['id']}")->find();
    $business = M('users')->where("shop_id={$order[shop_id]}")->find();
    $app_info = $this->app_info();

    if ($order && $data['prepay_id'] && $business) {
      if ($app_info['cappid'] && $app_info['new_order_template_id']) {
        $order['xcx_openid'] = $business['xcx_openid'];
        $order['template_id'] = $app_info['new_order_template_id'];
        $order['form_id'] = $data['prepay_id'];

        $order['body'] = "{$order_child['product_name']}等商品 / {$order['receiver_name']} / {$order['receiver_mobile']}";
        $order['created_at'] = date('Y-m-d H:i:s', $order['created_at']);
        $order['data'] = array(
          'keyword1' => array('value' => $order['tran_no'], 'color' => '#173177'),
          'keyword2' => array('value' => $order['body'], 'color' => '#173177'),
          'keyword3' => array('value' => $order['total_amount'], 'color' => '#173177'),
          'keyword4' => array('value' => '已支付', 'color' => '#173177'),
          'keyword5' => array('value' => $order['created_at'], 'color' => '#173177')
        );

        $result = send_app_msg($order, $this->app_id);

        if ($result) {
          return_data("success", "msg.new.order.send.success", $result);
        } else {
          return_data("eroor", "msg.new.order.send.error");
        }
      } else {
        return_data("error", "msg.new.order.data.error");
      }
    }
  }

  /**
   * [确认订单]
   * @return [type] [description]
   */
  public function confirm_order()
  {
    if (!$this->shop_id) {
      return_data("error", "confirm.order.error");
    }

    $payload = file_get_contents('php://input');
    $data = json_decode($payload, true);

    $map = array(
    'o.id' => $data['id'],
    'o.shop_id' => $this->shop_id,
    'o.status' => 1);
    $order = M('orders')->alias('o')
      ->join("left join ".C('DB_PREFIX')."users u on u.id=o.user_id")
      ->join("left join ".C('DB_PREFIX')."shops s on s.id=o.shop_id")
      ->field('o.*,u.xcx_openid, s.name shop_name')
      ->where($map)->find();
    $app_info = $this->app_info();

    if ($order) {
      $order_child = M('order_children')->where("order_id={$order['id']}")->find();
      $order['body'] = "{$order_child['product_name']}等商品";

      $order['template_id'] = $app_info['confirm_order_template_id'];
      $order['status'] = '已确认';
      $order['created_at'] = date('Y-m-d H:i:s', $order['created_at']);
      $order['data'] = array(
        'keyword1' => array('value' => $order['tran_no'], 'color' => '#173177'),
        'keyword2' => array('value' => $order['total_amount'], 'color' => '#173177'),
        'keyword3' => array('value' => $order['body'], 'color' => '#173177'),
        'keyword4' => array('value' => "{$order[province]} {$order[city]} {$order[area]} {$order[addr]}", 'color' => '#173177'),
        'keyword5' => array('value' => "{$order['receiver_name']} / {$order['receiver_mobile']}", 'color' => '#173177'),
        'keyword6' => array('value' => "店家：{$order['shop_name']}", 'color' => '#173177')
      );
      $result = send_app_msg($order, $this->app_id);

      if ($result) {
        return_data("success", "confirm.order.success", $result);
      } else {
        return_data("error", "confirm.order.error");
      }
    } else {
      return_data("error", "order.not.exist");
    }
  }
}
